package com.b2c.ftysys.dao.impl;

import com.b2c.ftysys.dao.DatabaseException;
import com.b2c.ftysys.dao.ProductDAO;
import com.b2c.ftysys.domain.ExampleFactory;
import com.b2c.ftysys.domain.Product;
import com.b2c.ftysys.domain.ProductExample;
import com.b2c.ftysys.helper.ProductUtils;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;
import org.apache.commons.lang.StringUtils;

@Repository
public class ProductDAOImpl extends SupportDAO<Product> implements ProductDAO {

	public ProductDAOImpl() {
		super();
	}

	public void save(Product param) {
		try {
			Assert.notNull(param);
			Assert.notNull(param.getCmtpdtskuid());
			Assert.notNull(param.getPdtskuid());

			if (StringUtils.isEmpty(param.getCmtpdtskuid()))
				throw new DatabaseException("客户SKU号不能为空");
			
			if (StringUtils.isEmpty(param.getPdtskuid()))
				throw new DatabaseException("工厂SKU号不能为空");

			if (null == param.getPdtStatus() || param.getPdtStatus() == 0) {
				param.setPdtStatus(Product.ON_READY);
			}

			if (StringUtils.isEmpty(param.getStyleno())) {
				param.setStyleno(ProductUtils.getStyleNoByCmtSku(param
						.getCmtpdtskuid()));
			}
			
			ProductExample example = ExampleFactory.createProductExample();
			example.createCriteria().andPdtskuidEqualTo(param.getPdtskuid());
			
			if(countByExample(example)>0){
				throw new DatabaseException("此订单产品已导入，请不要重复导入！");
			}
			
			param.setImportdate(new Date());
			this.save(param, "product.abatorgenerated_insert");
		} catch (DatabaseException e) {
			e.printStackTrace();
		}
	}

	public int updateByPrimaryKey(Product record) {
		int rows = 0;
		try {
			rows = update(record, "product.abatorgenerated_updateByPrimaryKey");
		} catch (DatabaseException e) {
			e.printStackTrace();
		}
		return rows;
	}

	/**
	 * This method was generated by Abator for iBATIS. This method corresponds
	 * to the database table pdtorder
	 * 
	 * @abatorgenerated Tue Jul 13 23:00:01 CST 2010
	 */
	public int updateByPrimaryKeySelective(Product record) {
		int rows = 0;
		// getSqlMapClientTemplate().update("product.abatorgenerated_updateByPrimaryKeySelective",
		// record);
		try {
			rows = update(record,
					"product.abatorgenerated_updateByPrimaryKeySelective");
		} catch (DatabaseException e) {
			e.printStackTrace();
		}
		return rows;
	}

	/**
	 * This method was generated by Abator for iBATIS. This method corresponds
	 * to the database table pdtorder
	 * 
	 * @abatorgenerated Tue Jul 13 23:00:01 CST 2010
	 */
	public List<Product> selectByExample(ProductExample example) {
		List<Product> list = null;
		try {
			list = super.getList(example,
					"product.abatorgenerated_selectByExample");
		} catch (DatabaseException e) {
			e.printStackTrace();
		}
		// (List<Product>)
		// getSqlMapClientTemplate().queryForList("product.abatorgenerated_selectByExample",
		// example);
		return list;
	}

	/**
	 * This method was generated by Abator for iBATIS. This method corresponds
	 * to the database table pdtorder
	 * 
	 * @abatorgenerated Tue Jul 13 23:00:01 CST 2010
	 */
	public Product selectByPrimaryKey(Long pdtorderid) {
		Product key = new Product();
		key.setPdtid(pdtorderid);
		Product record = null;
		try {
			record = super.queryWithId(key,
					"product.abatorgenerated_selectByPrimaryKey");
		} catch (DatabaseException e) {
			e.printStackTrace();
		}
		// (Product)
		// getSqlMapClientTemplate().queryForObject("product.abatorgenerated_selectByPrimaryKey",
		// key);
		return record;
	}

	/**
	 * This method was generated by Abator for iBATIS. This method corresponds
	 * to the database table pdtorder
	 * 
	 * @abatorgenerated Tue Jul 13 23:00:01 CST 2010
	 */
	public int deleteByExample(ProductExample example) {
		int rows = 0;
		try {
			rows = super.deleteByObject(example,
					"product.abatorgenerated_deleteByExample");
		} catch (DatabaseException e) {
			e.printStackTrace();
		}
		// getSqlMapClientTemplate().delete("product.abatorgenerated_deleteByExample",
		// example);
		return rows;
	}

	/**
	 * This method was generated by Abator for iBATIS. This method corresponds
	 * to the database table pdtorder
	 * 
	 * @abatorgenerated Tue Jul 13 23:00:01 CST 2010
	 */
	public int deleteByPrimaryKey(Long pdtorderid) {
		Product key = new Product();
		key.setPdtid(pdtorderid);
		int rows = 0;
		try {
			rows = super.delete(key,
					"product.abatorgenerated_deleteByPrimaryKey");
		} catch (DatabaseException e) {
			e.printStackTrace();
		}
		// getSqlMapClientTemplate().delete("product.abatorgenerated_deleteByPrimaryKey",
		// key);
		return rows;
	}

	/**
	 * This method was generated by Abator for iBATIS. This method corresponds
	 * to the database table pdtorder
	 * 
	 * @abatorgenerated Tue Jul 13 23:00:01 CST 2010
	 */
	public int countByExample(ProductExample example) {
		Integer count = 0;
		try {
			count = super.getCountByObjectForPage(example,
					"product.abatorgenerated_countByExample");
		} catch (DatabaseException e) {
			e.printStackTrace();
		}
		// (Integer)
		// getSqlMapClientTemplate().queryForObject("product.abatorgenerated_countByExample",
		// example);
		return count;
	}

	/**
	 * This method was generated by Abator for iBATIS. This method corresponds
	 * to the database table pdtorder
	 * 
	 * @abatorgenerated Tue Jul 13 23:00:01 CST 2010
	 */
	public int updateByExampleSelective(Product record, ProductExample example) {
		UpdateByExampleParms parms = new UpdateByExampleParms(record, example);
		int rows = 0;
		try {
			rows = super.updateByObject(parms,
					"product.abatorgenerated_updateByExampleSelective");
		} catch (DatabaseException e) {
			e.printStackTrace();
		}
		// getSqlMapClientTemplate().update("product.abatorgenerated_updateByExampleSelective",
		// parms);
		return rows;
	}

	/**
	 * This method was generated by Abator for iBATIS. This method corresponds
	 * to the database table pdtorder
	 * 
	 * @abatorgenerated Tue Jul 13 23:00:01 CST 2010
	 */
	public int updateByExample(Product record, ProductExample example) {
		UpdateByExampleParms parms = new UpdateByExampleParms(record, example);
		int rows = 0;
		try {
			rows = super.updateByObject(parms,
					"product.abatorgenerated_updateByExample");
		} catch (DatabaseException e) {
			e.printStackTrace();
		}
		return rows;
	}

	/**
	 * This class was generated by Abator for iBATIS. This class corresponds to
	 * the database table product
	 * 
	 * @abatorgenerated Tue Jul 13 23:00:01 CST 2010
	 */
	private static class UpdateByExampleParms extends ProductExample {
		private Object record;

		public UpdateByExampleParms(Object record, ProductExample example) {
			super(example);
			this.record = record;
		}

		@SuppressWarnings("unused")
		public Object getRecord() {
			return record;
		}
	}

	public static void main(String[] args) {
		String code = "S112839700001";

		String code1 = code.substring(1, 7);
		System.out.println(Integer.parseInt(code1));
	}
}